package cn.iocoder.yudao.module.infra.prometheus;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component
public class LLMConnectionMetrics {

    private final AtomicInteger activeConnections = new AtomicInteger(0);

    @Resource
    private MeterRegistry registry;

    public LLMConnectionMetrics() {
    }

    @PostConstruct
    public void init() {
        // 注册 Gauge 指标，名称自定义为 "websocket.connections.active"
        Gauge.builder("llm.connections.active", activeConnections, AtomicInteger::get)
                .description("当前活跃的 LLM 连接数")
                .tag("protocol", "http")  // 可选：添加标签区分协议类型
                .tag("provider", "ollama")  // 可选：添加标签区分协议类型
                .register(registry);
    }

    // 增加连接数
    public void incrementConnections() {
        activeConnections.incrementAndGet();
    }

    // 减少连接数
    public void decrementConnections() {
        activeConnections.decrementAndGet();
    }

}
